import { defineConfig, loadEnv } from "vite"
import vue from "@vitejs/plugin-vue"
import { resolve } from "path" //path 模块和 require 都是 Node.js 的东西，需要安装 Node.js 的声明文件，pnpm i @types/node -D
import pxToViewPort from "postcss-px-to-viewport" // 375自适应
import DefineOptions from "unplugin-vue-define-options/vite"
import AutoImport from "unplugin-auto-import/vite"
import Components from 'unplugin-vue-components/vite';
import { configSetParam } from "./src/utils"
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { VantResolver } from '@vant/auto-import-resolver';
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import ElementPlus from 'unplugin-element-plus/vite'

const pathSrc = resolve(__dirname, "src")
// https://vitejs.dev/config/
export default ({ mode }) => {
  return defineConfig({
    plugins: [
      vue(),
      ElementPlus({
        // options
      }),
      DefineOptions(), // 组件命名
      AutoImport({
        imports: ["vue", "vue-router", "pinia"], // 自动引入vue、vue-router、pinia
        resolvers: [
          VantResolver()
        ],
        dts: resolve(pathSrc, "auto-imports.d.ts"),
      }),
      Components({
        resolvers: [
          //自动导入 Element Plus 组件
          ElementPlusResolver({ importStyle: 'sass' }),
           //自动注册图标组件
           IconsResolver({
            enabledCollections: ['ep']
          }),
          VantResolver()
        ],
        dts: resolve(pathSrc, 'components.d.ts')
      }),
      Icons({
        autoInstall: true
      })
    ],
    css: {
      postcss: {
        plugins: [
          pxToViewPort({
            unitToConvert: "px", // 要转化的单位
            viewportWidth: configSetParam.viewportWidth, // UI设计稿宽度
          }),
        ],
      },
      preprocessorOptions: {
        // 全局变量引入
        scss: {
          javascriptEnabled: true,
        },
      },
    },
    resolve: {
      alias: {
        "@": pathSrc,
      },
    },
    build: {
      target: "modules",
      outDir: "dist",
      minify: "terser",
      sourcemap: false,
      chunkSizeWarningLimit: 1500,
      terserOptions: {
        compress: {
          drop_console: false,
          drop_debugger: true,
        },
        output: {
          comments: true,
        },
      },
      rollupOptions: {
        output: {
          manualChunks: {
            vue: ["vue", "vue-router", "axios"],
          },
        },
      },
    },
    base: loadEnv(mode, process.cwd()).VITE_BASE,
    server: {
      open: configSetParam.open,
      port: configSetParam.port,
      https: configSetParam.https,
      host: configSetParam.host,
      cors: configSetParam.cors,
      proxy: {
        // '/v3': {
        //   target: "https://api.coze.cn",
        //   changeOrigin: true,
        //   rewrite: (path) => path
        // },
        "/cozemp": {
          target: "https://www.wenshuiweilai.com",
          // target: "http://10.80.19.156:8888",
          // target:"http://192.168.2.51:18732",
          changeOrigin: true,
          rewrite: (path) => path,
        },
      },
    },
  })
}
